/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author TEComp
 */
public class MenuService {

    //private  dataSource = null;
    public void init() {
    }

    public static void main(String args[]) {
        try {
            Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/workforce", "postgres", "miracle2012");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from find_menu('r001', 0, null)");
            if (rs.next()) {
                String strMenu = rs.getString(1);
                System.out.println("strMenu[" + strMenu + "]");
                parser(strMenu, 0);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void parser(String token, int level) {
        //String token = "{'Product','#',{'DHTML Software','#',''},{'Flash Software','#',''},{'Web authoring Tool','#',''}},{'Template Service','#',{'Business Software','#',''},{'SWF Quicker','#',''},{'Glanda','#',''},{'SWF Decompiler','#',''}},{'Purchase','#',''},{'Support','#',''},{'Community','#',''},{'Forum','#',''}";
        int op = 0;
        //List<Menu> list = new ArrayList<Menu>();
        StringBuilder kp = new StringBuilder();
        String tokenlen  = "";
        if( level > 0 ) {
            tokenlen = token.substring(1, token.length()-1);
        } else {
            tokenlen = token;
        }
        StringBuilder sb = new StringBuilder(tokenlen);
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            switch(c) {
                case '{': op++; kp.append(c); break;
                case '}': op--; kp.append(c); break;
                case ',': { 
                    if( op == 0 ) {
                        System.out.println("["+ kp.toString() +"]");
                        //list.add(kp.toString());
                        kp.delete(0, kp.length());
                    } else {
                        kp.append(c);
                    }
                } break;
                default: kp.append(c);
                    break;
            }
        }
    }
}
